﻿using Microsoft.AspNetCore.Mvc.Filters;
using SimpleInOutControl.Common;

namespace SimpleInOutControl.Utility
{
    /// <summary>
    /// 自定义动作过滤器
    /// </summary>
    //public class CustomActionFilterAttribute : Attribute, IActionFilter
    public class CustomActionFilter : IActionFilter
    {
        /// <summary>
        /// 接口执行前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            //var ca = context.Controller.GetType();
            //var token = context.HttpContext.Request.Headers["Authorization"].ToString();

            //var sss = JwtUtils.ReadToken(token.Split(' ').LastOrDefault());
            //var sss = context.HttpContext.GetEndpoint().Metadata;
            //var ddd = sss.Where(u => u.GetType().Name == "NoLoginAttributes");

            var noLogin = context.HttpContext.GetEndpoint().Metadata.Where(u => u.GetType().Name == "NoLoginAttribute").ToArray().Length > 0;
            if (noLogin)
            {
                //直接访问不做处理
            }
            else
            {
                //校验token
                var token = context.HttpContext.Request.Headers["Authorization"].ToString();
                var result = JwtUtils.CheckToken(token.Split(' ').LastOrDefault());
            }
        }
        /// <summary>
        /// 接口执行后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            //context.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
        }
    }
}
